package com.study.spark.scala.chapter07

import scala.collection.mutable.ArrayBuffer

object Scala03_Collection_2{
  def main(args: Array[String]): Unit = {
    // TODO 集合 - 常用方法
    val array = ArrayBuffer(1,2,3,4)
    println(array.sum)//10
    println(array.max)//4
    println(array.min)//1
    println(array.product)//24

    // 自定义数据操作的方法：reduce
    // 计算原则：两两计算
    // map => reduce => 简化，规约（聚合）

    // 匿名函数 至简原则
//    println(array.reduce((x: Int, y: Int) => {x + y}))
//    println(array.reduce((x: Int, y: Int) => x + y))
//    println(array.reduce((x, y) => x + y))
//    println(array.reduce( _ + _))
    println(array.reduce( _ - _))//-8
    // 默认就是左聚合
    println(array.reduceLeft( _ - _))//-8

    val array1 = ArrayBuffer(1,2,3,4,5)
    // 右聚合：(1-(2-(3-(4-5))))
    println(array1.reduceRight( _ - _))//3

  }
}
